<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2021/5/10
 * Time: 9:01
 */
namespace app\admin\controller\company;

use app\common\controller\Backend;
use think\config;
use fast\Tool;

class Company extends Backend
{

    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Company');

    }

    public function index(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams('name');
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);


            return json($result);
        }
        return $this->view->fetch();
    }


    public function deploy(){
        $id = $this->request->param('ids');
        if(empty($id)){
            $this->error('请选择要部署的公司！');
        }
        $companyInfo = $this->model->where(['id'=>$id])->find();
        if(empty($companyInfo)){
            $this->error(__('所选公司不存在！', ''));
        }

        //分享口令
        $shareArr['douyin'] = $companyInfo['douyin'];
        $shareArr['kuaishou'] = $companyInfo['kuaishou'];
        $shareArr['taobao'] = $companyInfo['taobao'];
        $shareArr['tianmao'] = $companyInfo['tianmao'];
        $shareArr['albb'] = $companyInfo['albb'];

        //获取前端聊天样式
        $chatstyleInfo = getChatStyleInfo($id);

        $userInfo = model("User")->where(['company_id'=>$id])->find();
        if(empty($userInfo['id'])){
            $this->error(__('所选公司没有客服，请联系管理员设置客服！', ''));
        }

        $token = Tool::en_token($userInfo['id'],$id);
//        echo $token;
        $jsUrl = Tool::createJsCode($id, $token, $chatstyleInfo,$shareArr);

        return $this->success("ok","",$jsUrl);
    }


}